const isPhone = (value: string): boolean => {
  return /^1[123456789]\d{9}$/.test(value);
};

const isIdCardReg = (value: string): boolean => {
  return /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(value)
}

const isNumber = val => {
  var regPos = /^\d+(\.\d+)?$/; //非负浮点数
  var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
  if (regPos.test(val) && regNeg.test(val)) {
    return true;
  } else {
    return false;
  }
};

const isVideo = (url: string) => {
  const arr = new Array("avi", "wmv", "mpg", "mpeg", "mov", "rm", "ram", "swf", "flv", "mp4", "mp3", "wma", "avi", "rm", "rmvb", "flv", "mpg", "mkv")
  const reg = /^(.*)(\.)(.{1,8})$/

  const type = url.match(reg);
  //防止出问题，把获取到的所有结尾格式，全部转化为小写
  if (type && type[3]) {

    for (let i = 0; i < arr.length; i++) {
      if (arr[i] == type[3].toLowerCase()) {
        return true
      }
    }
  }

  return false

}

const isImage = (str: string) => {
  var reg = /\.(png|jpg|gif|jpeg|webp)$/;
  return reg.test(str);
}
const isPPT = (str: string) => {
  var reg = /\.(ppt)$/;
  return reg.test(str);
}

const isSceneExist = (scene: any) => {
  return scene !== undefined && scene !== "undefined";
}

const isIdCard = (idCard: string) => {
  if (idCard.length === 18) {
    return isIdCardReg(idCard)
  } else {
    return false
  }
}

const getFileTxt = (name: string) => {
  return name.substring(name.lastIndexOf("."))

}

export const CommonRegs = {
  isPhone,
  isNumber,
  isVideo,
  isImage,
  isIdCard,
  isPPT,
  isSceneExist,
  getFileTxt
};
